{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Coin toss on an actual IBM quantum computer\n",
    "In this exercise we run our simple coin toss circuit on a real IBM Q quantum computer. For a local Qiskit installation we need to store our IBM Q credentials locally to be able to access the IBM Q backends.\n",
    "We use the `least_busy` method to find the backend with the smallest queue."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Import the required libraries, including the IBM Q library for working with IBM Q hardware."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "from qiskit import QuantumCircuit, execute, Aer, IBMQ\n",
    "\n",
    "# Import Blochsphere visualization\n",
    "from qiskit.visualization import plot_histogram\n",
    "\n",
    "# Import the job monitoring tool\n",
    "from qiskit.tools.monitor import job_monitor"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "You must also load your API key into the system. This can be done by saving the account information locally. This is a one time load."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "#Save and store API key locally.\n",
    "#IBMQ.save_account('MY_API_TOKEN')\n",
    "\n",
    "IBMQ.load_account()\n",
    "provider = IBMQ.get_provider()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Create a quantum circuit with a Hadamard gate and a measurement gate."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "qc = QuantumCircuit(1,1)\n",
    "\n",
    "qc.h(0)\n",
    "qc.measure(0,0)\n",
    "print(qc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Grab the least busy IBM Q backend."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "from qiskit.providers.ibmq import least_busy\n",
    "backend = least_busy(provider.backends(filters=lambda x: not x.configuration().simulator))\n",
    "\n",
    "print(\"Backend:\", backend.name())\n",
    "print(\"Pending jobs:\", backend.status().pending_jobs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Create a quantum job for the circuit and the selected backend that runs just one shot to simulate a coin toss. Run the job and monitor the progress. Get and display the result; either 0 for up (base) or 1 for down (excited)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "job = execute(qc, backend, shots=1)\n",
    "job_monitor(job)\n",
    "\n",
    "counts  = job.result().get_counts(qc)\n",
    "print(counts)  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "That was the simple readout where we just looked at the 'counts' parameter of the returned job information. \n",
    "Let's take a look at the whole returned results:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(job.result())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
